package com.awesome.common.util.device

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.telephony.TelephonyManager
import androidx.annotation.RequiresPermission
import androidx.core.app.ActivityCompat

/**
 * Author: JFangZ
 * Email: janezhang1901@163.com
 * Date: 2021/5/13 17:23
 * Description:Util for phone and sim.
 */
object PhoneUtil {
    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/18 17:48
     * Description:获取网络运营商代码,中国移动 46000/46002/46007,中国联通 46001,中国电信 46003
     */
    @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
    fun getNetworkOperator(context: Context): String? {
        val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
                ?: return null
        return if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            null
        } else telephonyManager.networkOperator
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/18 17:48
     * Description:获取网络运营商的名字
     */
    @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
    fun getNetworkOperatorName(context: Context): String? {
        val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
                ?: return null
        return if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            null
        } else telephonyManager.networkOperatorName
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/18 17:48
     * Description:获取网络运营商的国家代码
     */
    @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
    fun getNetworkCountryIso(context: Context): String? {
        val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
                ?: return null
        return if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            null
        } else telephonyManager.networkCountryIso
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/18 17:48
     * Description:获取 SIM 卡运营商的名字
     */
    @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
    fun getSimOperator(context: Context): String? {
        val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
                ?: return null
        return if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            null
        } else telephonyManager.simOperator
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/18 17:48
     * Description:获取 SIM 卡运营商的代码,中国移动 46000/46002/46007,中国联通 46001,中国电信 46003
     */
    @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
    fun getSimOperatorName(context: Context): String? {
        val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
                ?: return null
        return if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            null
        } else telephonyManager.simOperatorName
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/18 17:48
     * Description:获取 SIM 卡运营商的国家代码
     */
    @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
    fun getSimCountryIso(context: Context): String? {
        val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
                ?: return null
        return if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            null
        } else telephonyManager.simCountryIso
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2020/11/18 17:48
     * Description:获取 SIM 卡网络类型
     */
    @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
    fun getNetworkType(context: Context): Int {
        val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
                ?: return TelephonyManager.NETWORK_TYPE_UNKNOWN
        return if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            TelephonyManager.NETWORK_TYPE_UNKNOWN
        } else telephonyManager.networkType
    }
}